package safego

var defaultRecoverGoroutine = func(err interface{}) {}

// Go 默认带 recover 捕获 panic 的协程并发
// recover 只能无法捕获子协程的 panic，容易导致程序异常
func Go(goroutine func(), customRecover func(err interface{})) {
	go func() {
		defer func() {
			if err := recover(); err != nil {
				customRecover(err)
			}
		}()
		goroutine()
	}()
}

// DefaultGo 使用默认的 recover 函数进行并发
func DefaultGo(goroutine func()) {
	Go(goroutine, defaultRecoverGoroutine)
}

func SetDefaultRecoverGoroutine(customRecover func(err interface{})) {
	defaultRecoverGoroutine = customRecover
}
